//
//  SubscribeHandler.m
//  mUBB
//
//  Created by Cosmin Stirbu on 5/11/13.
//  Copyright (c) 2013 Cosmin Stirbu. All rights reserved.
//

#import "SubscribeHandler.h"
#import "NotificationsProtocol.h"
#import "UserDefaultsManager.h"

@implementation SubscribeHandler

- (void)handleSuccess:(WSResponse *)response {
    [self saveSubscribtionId];
    
    // Success
    [self.delegate didSubscribeWithSuccessForTicket:self.ticket];
}

- (void)saveSubscribtionId {
    NSDictionary *jsonDictionary = [_response.data objectFromJSONData];
    
    // Subscription Id
    NSNumber *subscriptionId = [jsonDictionary getJSONObjectForKey:ID_TAG];
    
    // Save Subscription Id
    [UserDefaultsManager saveObject:subscriptionId forKey:_course.courseCode];
}

- (void)handleError:(WSError *)error {
    if (error.errorCode == StatusCodeConflict) {
        [self saveSubscribtionId];
    }
    // Errors
    [self.delegate didSubscribeWithFailure:error forTicket:self.ticket];
}

@end
